'use strict';

const Service = require('egg').Service;

class UserService extends Service {
  async findById(uid) {
    const user = await this.app.mysql.select('user', {
      where: { id: uid },
      columns: ['id', 'name', 'username']
     });
    return user[0];
  }

  async findByUsername(username) {
    const user = await this.app.mysql.select('user', {
      where: { username: username },
      columns: ['id', 'name', 'username']
     });
    return user[0];
  }

  async password(username) {
    const pwd = await this.app.mysql.select('user', {
      where: { username: username },
      columns: ['password']
     });
    return pwd[0].password;
  }

  async findAll() {
    const users = await this.app.mysql.select('user', {
      columns: ['id', 'name', 'username']
    });
    // const name = await app.mysql.get('user', { id: 1 });
    return users;
  }

  async create(user) {
    const result = await this.app.mysql.insert('user', user);
    return result;
  }

  async update(uid, uname) {
    const result = await this.app.mysql.update('user', { id: uid, name: uname });
    return result;
  }
}

module.exports = UserService;
